// stores/host.js
import { defineStore } from 'pinia'
import { getHostInfo, addHostInfo } from '@/api/host'


export const useHostStore = defineStore('host', {
  state: () => ({
    hostData: [],
    isLoading: false,
  }),

  actions: {
    // 加载主机数据
    async loadHostData() {
      this.isLoading = true
      try {
        const response = await getHostInfo()
        this.hostData = response.data.records
      } catch (error) {
        console.error('加载主机列表失败', error)
      } finally {
        this.isLoading = false
      }
    },

    // 添加新主机
    async addHost(newHost) {
      // 调用后端接口添加主机
      try {
        const { data } = await addHostInfo(newHost)
        if (data.code === 0) {
          this.hostData.push(newHost)  // 添加新主机到列表
        }
      } catch (error) {
        console.error('添加主机失败', error)
      }
    },
  },
})
